﻿// <copyright file="DateoffsetConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-03</date>
// <summary>Date offset converter</summary>

namespace NYTimes.ValueConverters
{
    using System;
    using System.Windows;
    using System.Windows.Data;

    /// <summary>
    /// A value converter to convert dates to their offset equivalent
    /// </summary>
    public class DateOffsetConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Converts from a date to a date with a specific days offset
        /// </summary>
        /// <param name="value">The source data being passed to the target </param>
        /// <param name="targetType">The Type of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property. </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var date = (DateTime)value;

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            double days = double.Parse(parameter.ToString(), culture);

            date = date.AddDays(days);

            return date;
        }

        /// <summary>
        /// Not implemented
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The Type of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic. </param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the source object.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }

        #endregion
    }
}
